//
//  SXJFViewController.m
//  YaDiuDiu
//
//  Created by 欧洪飞 on 2017/8/30.
//  Copyright © 2017年 jy. All rights reserved.
//

#import "SXJFViewController.h"

#import "CircleView.h"
#import "JFCell.h"
#import "ZJFViewController.h"
@interface SXJFViewController ()<UITableViewDelegate,UITableViewDataSource>{
    CAShapeLayer *shapeLayer;
    //NSTimer *timer;
    NSMutableArray *array;
}

@property (nonatomic,strong)UITableView *tableView;

@end

@implementation SXJFViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    [self loadNav];
    array = [NSMutableArray array];
    [self loadData];
    [self loadUI];
    
}

-(UIStatusBarStyle)preferredStatusBarStyle{
    return UIStatusBarStyleLightContent;
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self preferredStatusBarStyle];
    
}

- (void)loadNav{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 64)];
    view.backgroundColor = COLOR_RGB(54, 59, 61, 1);
    view.userInteractionEnabled = YES;
    [self.view addSubview:view];
    
    UILabel *lab = [[UILabel alloc] init];
    lab.text = @"授信积分";
    lab.textColor = [UIColor whiteColor];
    lab.font = [UIFont systemFontOfSize:18.0];
    [view addSubview:lab];
    [lab makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(view.centerX);
        make.top.equalTo(34);
    }];
    
    //返回按钮
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    //btn.backgroundColor = [UIColor redColor];
    [btn setImage:[UIImage imageNamed:@"bjt"] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"bjt"] forState:UIControlStateHighlighted];
    [btn addTarget:self action:@selector(clickBack) forControlEvents:UIControlEventTouchUpInside];
    [view addSubview:btn];
    [btn makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(lab.centerY);
        make.left.equalTo(0);
        make.width.equalTo(40);
        make.height.equalTo(40);
    }];
}
// 返回
- (void)clickBack{
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)loadUI{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, ScreenHeight-64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 64, ScreenWidth, 400)];
    view1.backgroundColor = COLOR_RGB(54, 59, 61, 1);
    _tableView.tableHeaderView = view1;
    
    UILabel *lab1 = [[UILabel alloc] init];
    lab1.text = @"1029";
    lab1.textColor = [UIColor whiteColor];
    lab1.font = [UIFont systemFontOfSize:38.0];
    [view1 addSubview:lab1];
    [lab1 makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(view1.centerX);
        make.top.equalTo(view1.top).offset(20);
    }];
    
    UILabel *lab2 = [[UILabel alloc] init];
    lab2.text = @"我的积分";
    lab2.textColor = [UIColor whiteColor];
    lab2.font = [UIFont systemFontOfSize:15.0];
    [view1 addSubview:lab2];
    [lab2 makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(lab1.centerX);
        make.top.equalTo(lab1.bottom).offset(0);
    }];
    
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"赚积分" forState:UIControlStateNormal];
    [btn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [btn setImage:[UIImage imageNamed:@"zjf"] forState:UIControlStateNormal];
    [btn setTitleEdgeInsets:UIEdgeInsetsMake(0, -btn.imageView.frame.size.width-30, 0, btn.imageView.frame.size.width)];
    [btn setImageEdgeInsets:UIEdgeInsetsMake(0, btn.titleLabel.frame.size.width+60, 0, -btn.titleLabel.frame.size.width)];
    btn.layer.cornerRadius = 18.0;
    btn.layer.borderWidth = 1.0;
    btn.layer.borderColor = [UIColor whiteColor].CGColor;
    [btn addTarget:self action:@selector(clickZJF) forControlEvents:UIControlEventTouchUpInside];
    [view1 addSubview:btn];
    [btn makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(lab1.centerX);
        make.top.equalTo(lab2.bottom).offset(32);
        make.width.equalTo(120);
        make.height.equalTo(36);
    }];
    
    UILabel *line1 = [[UILabel alloc] initWithFrame:CGRectMake(0, 200, ScreenWidth, 10)];
    line1.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [view1 addSubview:line1];
    
    UILabel *line2 = [[UILabel alloc] initWithFrame:CGRectMake(0, 390, ScreenWidth, 10)];
    line2.backgroundColor = COLOR_RGB(242, 242, 242, 1);
    [view1 addSubview:line2];
    
    UIView *view2 = [[UIView alloc] initWithFrame:CGRectMake(0, view1.frame.origin.y+210, ScreenWidth, 180)];
    view2.backgroundColor = [UIColor whiteColor];
    [view1 addSubview:view2];
    
    UILabel *labJF = [[UILabel alloc] init];
    labJF.text = @"本月积分";
    labJF.font = [UIFont systemFontOfSize:15.0];
    labJF.textColor = [UIColor darkGrayColor];
    [view2 addSubview:labJF];
    [labJF makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(view2.left).offset(16);
        make.top.equalTo(view2.top).offset(16);
    }];
    
    UILabel *line3 = [[UILabel alloc] initWithFrame:CGRectMake(0, 50, ScreenWidth, 0.5)];
    line3.backgroundColor = [UIColor grayColor];
    [view2 addSubview:line3];
    //圆环
    CircleView *view = [[CircleView alloc] initWithFrame:CGRectMake(50, 80, 80, 80)];
    [view2 addSubview:view];
    [view setLineWidth:10.0];
    [view setValue:0.1];
    [view setLineColor:[UIColor blueColor]];
    
    UILabel *labY1 = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth/2, line3.frame.origin.y+40, 8, 8)];
    labY1.backgroundColor = [UIColor redColor];
    labY1.layer.cornerRadius = 5;
    labY1.layer.masksToBounds = YES;
    [view2 addSubview:labY1];
    
    UILabel *labY2 = [[UILabel alloc] initWithFrame:CGRectMake(ScreenWidth/2, line3.frame.origin.y+70, 8, 8)];
    labY2.backgroundColor = [UIColor blueColor];
    labY2.layer.cornerRadius = 5;
    labY2.layer.masksToBounds = YES;
    [view2 addSubview:labY2];
    
    UILabel *labxf = [[UILabel alloc] init];
    labxf.text = @"平台消费";
    labxf.font = [UIFont systemFontOfSize:15.0];
    labxf.textColor = [UIColor darkGrayColor];
    [view2 addSubview:labxf];
    [labxf makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(labY1.centerY);
        make.left.equalTo(labY1.right).offset(16);
    }];
    
    UILabel *labxfP = [[UILabel alloc] init];
    labxfP.text = @"123";
    labxfP.font = [UIFont systemFontOfSize:15.0];
    labxfP.textColor = [UIColor darkGrayColor];
    [view2 addSubview:labxfP];
    [labxfP makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(labY1.centerY);
        make.left.equalTo(labxf.right).offset(16);
    }];
    
    UILabel *labqd = [[UILabel alloc] init];
    labqd.text = @"每日签到";
    labqd.font = [UIFont systemFontOfSize:15.0];
    labqd.textColor = [UIColor darkGrayColor];
    [view2 addSubview:labqd];
    [labqd makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(labY2.centerY);
        make.left.equalTo(labY2.right).offset(16);
    }];
    
    UILabel *labqdP = [[UILabel alloc] init];
    labqdP.text = @"160";
    labqdP.font = [UIFont systemFontOfSize:15.0];
    labqdP.textColor = [UIColor darkGrayColor];
    [view2 addSubview:labqdP];
    [labqdP makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(labY2.centerY);
        make.left.equalTo(labqd.right).offset(16);
    }];
    
    UILabel *line4 = [[UILabel alloc] init];
    line4.backgroundColor = [UIColor grayColor];
    [view2 addSubview:line4];
    [line4 makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(labY2.left);
        make.right.equalTo(view2.right);
        make.top.equalTo(labqd.bottom).offset(8);
        make.height.equalTo(0.5);
    }];
    
    UILabel *labsum = [[UILabel alloc] init];
    labsum.text = @"总计";
    labsum.font = [UIFont boldSystemFontOfSize:14.0];
    labsum.textColor = [UIColor darkGrayColor];
    [view2 addSubview:labsum];
    [labsum makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line4.bottom).offset(4);
        make.left.equalTo(labqd.left);
    }];
    
    UILabel *labsumP = [[UILabel alloc] init];
    labsumP.text = @"283";
    labsumP.font = [UIFont systemFontOfSize:15.0];
    labsumP.textColor = [UIColor darkGrayColor];
    [view2 addSubview:labsumP];
    [labsumP makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(line4.bottom).offset(4);
        make.centerX.equalTo(labqdP.centerX);
    }];
    
}
//赚积分
- (void)clickZJF{
    ZJFViewController *zjf = [[ZJFViewController alloc] init];
    [self.navigationController pushViewController:zjf animated:YES];
}

#pragma -mark UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellId = @"Cell";
    JFCell *cell = [tableView dequeueReusableCellWithIdentifier:cellId];
    if (!cell) {
        cell = [[[NSBundle mainBundle] loadNibNamed:@"JFCell" owner:nil options:nil] lastObject];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    NSDictionary *op = array[indexPath.row];
    cell.labQD.text = [op objectForKey:@"remarks"];
    NSString *direc = [NSString stringWithFormat:@"%li",[[op objectForKey:@"directionofchange"] integerValue]];
    NSString *changenum = [NSString stringWithFormat:@"%li",[[op objectForKey:@"changenum"] integerValue]];
    if ([direc integerValue] == 2) {
        cell.labNum.text = [NSString stringWithFormat:@"+%@",changenum];
    }else{
        cell.labNum.text = [NSString stringWithFormat:@"-%@",changenum];
    }
    
    cell.labDate.text = [NSString stringWithFormat:@"%li",[[op objectForKey:@"addtime"] integerValue]];
    
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 60.0;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, 40.0)];
    view.backgroundColor = [UIColor whiteColor];
    
    UILabel *lab = [[UILabel alloc] initWithFrame:CGRectMake(16, 10, 100, 20)];
    lab.text = @"积分记录";
    lab.textColor = COLOR_RGB(51, 51, 51, 1);
    lab.font = [UIFont systemFontOfSize:15.0];
    [view addSubview:lab];
    
    UILabel *labLine = [[UILabel alloc] initWithFrame:CGRectMake(0, 39, ScreenWidth, 0.5)];
    labLine.backgroundColor = [UIColor grayColor];
    [view addSubview:labLine];
    
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40.0;
}
//用户授信积分详情页面
- (void)loadData{
    [SVProgressHUD show];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    NSMutableDictionary *parmas = [NSMutableDictionary dictionary];
    NSString *str = [NSString stringWithFormat:@"%@890d69e502d1d37a9e84335ccefece57",[user objectForKey:@"token2"]];
    NSString *token = [LCMD5Tool md5:str];
    
    [parmas setObject:token forKey:@"verifyToken"];
    [parmas setObject:[user objectForKey:@"userid"] forKey:@"userid"];
    
    NSString *url = [NSString stringWithFormat:@"%@/App/Usercenter/creditScore",urlHead];
    [[AFHTTPSessionManager manager] POST:url parameters:parmas success:^(NSURLSessionDataTask *task, id responseObject) {
        NSLog(@"授信积分详情res:%@",responseObject);
        NSString *state = [NSString stringWithFormat:@"%li",[[responseObject objectForKey:@"status"] integerValue]];
        if ([state integerValue] == 200) {
            array = [responseObject objectForKey:@"content"];
        }
        
        
        [_tableView reloadData];
        [SVProgressHUD dismiss];
    } failure:^(NSURLSessionDataTask *task, NSError *error) {
        NSLog(@"error:%@",error);
        [SVProgressHUD dismiss];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
